package com.midou.administration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.midou.administration.bo.*;
import com.midou.administration.entity.Desk;
import com.midou.administration.entity.DeskType;
import com.midou.administration.entity.Menu;
import com.midou.administration.mapper.MenuMapper;
import com.midou.administration.mapper.SetMealMapper;
import com.midou.administration.service.MenuService;
import com.midou.administration.utils.QRCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MrChen
 * @since 2021-03-24
 */
@Service
public class MenuServiceImpl implements MenuService {

  @Autowired
  private MenuMapper menuMapper;
  @Autowired
  private SetMealMapper setMealMapper;

  private String staticPath = System.getProperty("user.dir")+"\\midou-restaurant-administration\\src\\main\\resources\\static\\image";
  //查找全部菜品信息
  public Page findAll(MenuPagination menuPagination){
    List<Menu> list = menuMapper.findAll(menuPagination.getPage());
    menuPagination.getPage().setRecords(list);
    for (Object menus: list) {
      Menu menu = (Menu) menus;
      menu.setMenuImg(QRCodeUtils.ImageToBase64ByLocal(staticPath+"\\"+menu.getMenuImg()));
    }
    return menuPagination.getPage();
  }

  //判断商品名称是否存在
  public int judgmentMenuName(String menuName){
    int count = menuMapper.judgmentMenuName(menuName);
    return count;
  }

  //按照商品名称查找全部信息
  public List<Menu> findMenuName(String name){
    List<Menu> list = menuMapper.findMenuName(name);
    for (Object menus: list) {
      Menu menu = (Menu) menus;
      menu.setMenuImg(QRCodeUtils.ImageToBase64ByLocal(staticPath+"\\"+menu.getMenuImg()));
    }
    return list;
  }

  //更新菜品信息
  @Transactional(propagation = Propagation.REQUIRED)
  public int updMenu(Menu menu){
    if (menu.getMenuImg() != "" || menu.getMenuImg() != null) {
      String imgName = menuMapper.findMenuOne(menu.getMenuId());
      if (!menu.getMenuImg().equals(imgName)) {
          System.out.println(menu.getImgBase64());
          menu.setMenuImg(QRCodeUtils.randomImgName(menu.getMenuImg()));
          QRCodeUtils.GenerateImage(menu.getImgBase64(), staticPath + "/" + menu.getMenuImg(), menu.getMenuImg());
      }
    }
    int count = menuMapper.updateById(menu);
    return count;
  }

  //添加菜品信息
  public int istMenu(Menu menu){
    menu.setMenuImg(QRCodeUtils.randomImgName(menu.getMenuImg()));
    int count = menuMapper.istMenu(menu);
    if (count>0){
      boolean whetherSucceed = QRCodeUtils.GenerateImage(menu.getImgBase64(),staticPath+"/"+menu.getMenuImg(),menu.getMenuImg());
    }
    return count;
  }
  //按照id删除商品
  public int delMenu(int id){
    return menuMapper.delMenu(id);
  }
  //更新是否热门
  public int updPopulaTrue(int id){
    int num = menuMapper.updPopulaTrue(id);
    return num;
  }
  //更新是否热门
  public int updPopulaFalse(int id){
    int num = menuMapper.updPopulaFalse(id);
    return num;
  }

  //更新是否推荐
  public int updrecommendFalse(int id){
    int num = menuMapper.updrecommendFalse(id);
    return num;
  }
  //更新是否推荐
  public int updrecommendTrue(int id){
    int num = menuMapper.updrecommendTrue(id);
    return num;
  }
  //更新是否打折
  public int upddiscountTrue(int id){
    int num = menuMapper.upddiscountTrue(id);
    return num;
  }
  //更新是否打折
  public int upddiscountFalse(int id){
    int num = menuMapper.upddiscountFalse(id);
    return num;
  }
  //更新是否上架
  public int updputontheshelfTrue(int id){
    int num = menuMapper.updputontheshelfTrue(id);
    return num;
  }

  //更新是否上架
  public int updputontheshelfFalse(int id){
    int num = menuMapper.updputontheshelfFalse(id);
    return num;
  }

  //按照id查询菜品信息
  public Menu findIdMenu(int id){
    Menu menu = menuMapper.findIdMenu(id);
    return menu;
  }

  public Page queryAll(MenuBofenye menuBofenye){
    List<MenuBo> list=menuMapper.queryAll(menuBofenye.getPage());
    menuBofenye.getPage().setRecords(list);
    return menuBofenye.getPage();
  }

  public MenuBo queryId(Integer id){
    MenuBo menuBo = menuMapper.queryId(id);
    return menuBo;
  }

  public int queryUp(MenuBo menuBo){
    int num = menuMapper.queryUp(menuBo);
    return num;
  }


  @Override
  public List<Menu> queryBybusinessAndMenu(int  desk) {
    List<Menu> list = this.menuMapper.queryBybusinessAndDishType(desk);
    for (Menu menu : list) {
       menu.setMenuImg(QRCodeUtils.ImageToBase64ByLocal(staticPath+"\\"+menu.getMenuImg()));
    }
    return list;
  }

  @Override
  public Page queryAllSetMeal(QuerySetMealPageBo qsmpb) {
      QueryWrapper qw = new QueryWrapper();
      qw.eq("menu_if_meal",qsmpb.getIfGetMeal());
      qw.eq("business_id",qsmpb.getBid());
      Page page = menuMapper.selectPage(qsmpb.getPage(),qw);
      for (Object menus: page.getRecords()) {
        Menu menu = (Menu) menus;
        menu.setImgBase64(QRCodeUtils.ImageToBase64ByLocal(staticPath+"\\"+menu.getMenuImg()));
      }
      return page;
  }

  @Override
  public Menu queryMenuOne(Integer mid) {
    QueryWrapper qw = new QueryWrapper();
    qw.eq("menu_id",mid);
    Menu menu = menuMapper.selectOne(qw);
    menu.setImgBase64(QRCodeUtils.ImageToBase64ByLocal(staticPath+"\\"+menu.getMenuImg()));
    return menu;
  }

  @Override
  public List<Menu> queryMenu(menuGroupIdBo mgb) {
    return menuMapper.queryDishes(mgb);
  }

  @Override
  public List<Menu> queryAllMenu(QuerySetMealPageBo qsmpb) {
    QueryWrapper qw = new QueryWrapper();
    qw.eq("menu_if_meal",qsmpb.getIfGetMeal());
    qw.eq("business_id",qsmpb.getBid());
    return menuMapper.selectList(qw);
  }

  @Override
  @Transactional(propagation = Propagation.REQUIRED)
  public Integer addSetMeal(MenuBo mb) {
    mb.setMenuImg(QRCodeUtils.randomImgName(mb.getMenuImg()));
    Integer count = menuMapper.addSetMeal(mb);
    Integer num = setMealMapper.add(mb.getMenuName(), mb.getMenuId(), mb.getSetMealTable());
    if (count > 0) {
      boolean whetherSucceed = QRCodeUtils.GenerateImage(mb.getImgBase64(), staticPath + "/" + mb.getMenuImg(), mb.getMenuImg());
      if (whetherSucceed) {
        return num;
      }
    }
    throw new RuntimeException("失败");
  }

  @Override
  public Integer groupRepeatVerification(menuGroupIdBo mgb) {
    QueryWrapper qw = new QueryWrapper();
    qw.eq("menu_group",mgb.getMenuGroup());
    qw.eq("business_id",mgb.getBid());
    return menuMapper.selectCount(qw);
  }

  /**
   * 这是删除套餐的方法
   * @param mid 套餐的id
   * @return
   */
  @Override
  @Transactional(propagation = Propagation.REQUIRED)
  public Integer delMenuInSetMeal(Integer mid) {
    String imgName = menuMapper.findMenuOne(mid);
    Integer count = menuMapper.delMenuInSetMeal(mid);
    Integer count1 = setMealMapper.delSetMeal(mid);
    if (count>0&&count1>0){

      File file = new File(staticPath+"\\"+imgName);
      if (file.exists()){
        file.delete();
      }
      return 1;
    }
    return 0;
  }
  @Transactional(propagation = Propagation.REQUIRED)
  public Integer modifyMenuInSetMeal(MenuBo mb) {
    if (mb.getMenuImg() != "" || mb.getMenuImg() != null) {
      String imgName = menuMapper.findMenuOne(mb.getMenuId());
      if (!mb.getMenuImg().equals(imgName)) {
        boolean result = QRCodeUtils.removeFile(staticPath, imgName);
        if (result) {
          mb.setMenuImg(QRCodeUtils.randomImgName(mb.getMenuImg()));
          QRCodeUtils.GenerateImage(mb.getImgBase64(), staticPath + "/" + mb.getMenuImg(), mb.getMenuImg());
        }
      }
    }
    Integer setMealCount = setMealMapper.queryCount(mb.getMenuId());
    Integer number = setMealMapper.delSetMeal(mb.getMenuId());

    if (number > 0 || setMealCount == 0) {
      for (Menu menu:mb.getSetMealTable()) {
        System.out.println(menu.getMenuGroup());
      }
      Integer count = menuMapper.modifyMenuInSetMeal(mb);
      Integer num = setMealMapper.add(mb.getMenuName(), mb.getMenuId(), mb.getSetMealTable());
      if (count <= 0) {
        throw new RuntimeException("更新失败!");
      }
    }
    return 1;
  }

  @Override
  public Integer updateShelf(CommonResult cr) {
    return menuMapper.updateShelf(cr);
  }

  @Override
  public Integer whetherRecommend(CommonResult cr) {
    return menuMapper.whetherRecommend(cr);
  }


}
